package com.yxx.sys.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
/**
 * @author wujiaxing
 * <p>
 * 使用Swagger2只需三步
 * 1、导入Swaggerr依赖
 * 2、配置Docket的bean
 * 3、使用@Api等注解修饰
 * </p>
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

  @Bean
  public Docket createRestApi() {
    //添加header参数
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            // 方法需要有ApiOperation注解才能生存接口文档
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            // 路径使用any风格
            .paths(PathSelectors.any())
            .build()
            // 如何保护我们的Api，有三种验证（ApiKey, BasicAuth, OAuth）
            .securitySchemes(security())
            // 接口文档的基本信息
            .apiInfo(apiInfo());
  }

  /**
   * 接口文档详细信息
   *
   * @return
   */
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("俞鑫鑫").description("仓库管理系统的接口文档").termsOfServiceUrl("http://www.localhost:8002").version("1.0.0").build();
  }

  private List<ApiKey> security() {
    ArrayList<ApiKey> apiKeys = new ArrayList<>();
    apiKeys.add(new ApiKey("Authorization", "Authorization", "header"));
    return apiKeys;
  }
}